package cn.javabb.web.controller;

import cn.javabb.core.base.BaseController;
import cn.javabb.core.entity.biz.PostDO;
import cn.javabb.core.entity.biz.CatalogDO;
import cn.javabb.core.entity.sys.LinkDO;
import cn.javabb.core.entity.sys.UserDO;
import cn.javabb.core.service.biz.PostService;
import cn.javabb.core.service.biz.CatalogService;
import cn.javabb.core.service.sys.LinkService;
import cn.javabb.core.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.Map;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/5/005 16:29
 * @Since V1.0
 */
@Controller
@RequestMapping("/")
public class IndexController extends BaseController {

    private static final String INDEX_URL = "web/index";

    @Autowired
    private CatalogService catalogService;
    @Autowired
    private PostService postService;
    @Autowired
    private UserService userService;
    @Autowired
    private LinkService linkService;
    /**
     * 首页数据
     * @return
     */
    public void loadIndexData(Model model){
        /*List<NavDO> listNavNavs = navService.listNavNavs();
        model.addAttribute("navNavs",listNavNavs);*/
        /*首页分类信息*/
        List<CatalogDO> listCatalogs = catalogService.listIndexCatalogs();
        model.addAttribute("catalogs",listCatalogs);
        // 首页文章归类
        Map<String,List<PostDO>> mapListPost = postService.mapListPosts(6);
        model.addAttribute("mapListPost",mapListPost);
        // 用户排名
        List<UserDO> rankUsers = userService.listRankUsers(8);
        model.addAttribute("rankUsers",rankUsers);
        // 论坛统计
        Map<String,Object> mapCount = postService.mapCount();
        // 加载配置
        model.addAttribute("mapCount",mapCount);
        // 友情链接
        List<LinkDO> listLinks = linkService.list();
        model.addAttribute("links",listLinks);
    }

    @GetMapping({"/","/index"})
    public String toIndex(Model model){
        loadIndexData(model);
        return INDEX_URL;
    }
}
